import os
import sys
import numpy as np

loss = 1e-4 # 容忍偏差，一般fp16要求绝对误差和相对误差均不超过千分之一
minimum = 10e-10

def verify_result(real_result, golden):
    real_result = np.fromfile(real_result, dtype=np.float) # 从bin文件读取实际运算结果
    golden = np.fromfile(golden, dtype=np.float) # 从bin文件读取预期运算结果
    different = []
    for idx in range(len(golden)):
        if abs(golden[idx] - real_result[idx])/max(abs(golden[idx]),abs(real_result[idx])) > loss:
                different.append(idx)
    if different:
        print("total.:{} \n".format(len(different)))
        print("real_result.:.{} \n".format(real_result))
        print("gold.:.{}\n".format(golden))
        print("different:{}\n golden:{}\n, real_result:{}\n".format(different[0:10],golden[different[0:10]],real_result[different[0:10]]))
    result = np.abs(real_result - golden) # 计算运算结果和预期结果偏差
    deno = np.maximum(np.abs(real_result), np.abs(golden))  # 获取最大值并组成新数组
    result_atol = np.less_equal(result, loss) # 计算绝对误差
    result_rtol = np.less_equal(result / np.add(deno, minimum), loss) # 计算相对误差
    if not result_rtol.all() and not result_atol.all():
        if np.sum(result_rtol == False) > real_result.size * loss and np.sum(result_atol == False) > real_result.size * loss: # 误差超出预期时返回打印错误，返回对比失败
            print("[ERROR] result error")
            return False
    print("test pass")
    return True

if __name__ == '__main__':
    verify_result(sys.argv[1],sys.argv[2])
